package com.bw.zg2.arouter

import android.content.Context
import android.text.TextUtils
import com.alibaba.android.arouter.facade.Postcard
import com.alibaba.android.arouter.facade.annotation.Interceptor
import com.alibaba.android.arouter.facade.callback.InterceptorCallback
import com.alibaba.android.arouter.facade.template.IInterceptor
import com.alibaba.android.arouter.launcher.ARouter
import com.bw.zg2.utils.SpUtil

/**
 *
 * @ClassName: LoginInterceptor
 * @Description: 通过arouter实现登录拦截器
 * @Author: zhangyanchao
 * @CreateDate: 2024/9/20 2:58 PM
 * @Version: 1.0
 */
@Interceptor(priority = 1, name = "LoginInterceptor")
class LoginInterceptor : IInterceptor {
    override fun init(context: Context?) {
    }

    override fun process(postcard: Postcard?, callback: InterceptorCallback?) {
        //先判断用户是否登录
        val token = SpUtil.getString("token")
        if (TextUtils.isEmpty(token) && !postcard?.path.equals("/login/LoginActivity")) {
            //如果用户未登录，且当前要跳转的页面不是登录页面，则进行拦截，跳转到登录页面
            //拦截 /main/MainActivity
            ARouter.getInstance()
                .build("/login/LoginActivity")
                .greenChannel()
                .navigation()
            callback?.onInterrupt(null)
        }else{
            //如果已经登录，则继续路由跳转
            callback?.onContinue(postcard)
        }
    }

}